package com.woniu.venue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.venueCertify.VenueCertifyException;
import com.woniu.exception.venueCertify.VenueCertifyExceptionCode;
import com.woniu.model.Result;
import com.woniu.util.StateUtil;
import com.woniu.venue.client.AccountClient;
import com.woniu.venue.client.model.AddVenueAdminForm;
import com.woniu.venue.client.model.VenueAdminDto;
import com.woniu.venue.dto.venueCertify.VenueCertifyDto;
import com.woniu.venue.dto.venueInfo.VenueInfoDto;
import com.woniu.venue.model.VenueCertify;
import com.woniu.venue.dao.VenueCertifyDao;
import com.woniu.venue.param.venueCertify.CreatVenueCertifyParam;
import com.woniu.venue.param.venueCertify.LoadVenueCertifyParam;
import com.woniu.venue.param.venueCertify.RefuseVenueCertifyParam;
import com.woniu.venue.param.venueInfo.AddVenueInfoParam;
import com.woniu.venue.service.VenueCertifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.venue.service.VenueInfoService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@Service
public class VenueCertifyServiceImpl extends ServiceImpl<VenueCertifyDao, VenueCertify> implements VenueCertifyService {

    @Autowired
    private VenueInfoService venueInfoService;
    @Autowired
    private AccountClient accountClient;
    @Autowired
    private VenueCertifyDao venueCertifyDao;


    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public VenueCertifyDto creat(CreatVenueCertifyParam param) {
        //判断是否在申请中
        QueryWrapper<VenueCertify> qw = new QueryWrapper<VenueCertify>().eq("venue_phone", param.getVenuePhone());
        List<VenueCertify> venueCertifies = venueCertifyDao.selectList(qw);
        if (venueCertifies==null||venueCertifies.size()==0){
            //先生成场馆信息
            AddVenueInfoParam addVenueInfoParam = new AddVenueInfoParam();
            addVenueInfoParam.setVenueAddress(param.getVenueAddress());
            addVenueInfoParam.setVenueImages(param.getVenueImgs());
            addVenueInfoParam.setVenueLicenses(param.getVenueLicense());
            addVenueInfoParam.setVenueName(param.getVenueName());
            VenueInfoDto venueInfoDto = venueInfoService.addVenueInfo(addVenueInfoParam);
            //生成场馆管理员
            AddVenueAdminForm addVenueAdminForm = new AddVenueAdminForm();
            addVenueAdminForm.setVenueAdminImg(param.getVenueImgs().get(0));
            addVenueAdminForm.setVenueAdminName(param.getVenueAdminName());
            addVenueAdminForm.setVenueAdminPhone(param.getVenuePhone());
            addVenueAdminForm.setVenueId(venueInfoDto.getVenueId());
            ArrayList<Integer> integers = new ArrayList<>();
            integers.add(1);
            integers.add(2);
            integers.add(3);
            addVenueAdminForm.setVenueAdminRole(integers);
            Result<VenueAdminDto> venueAdminDtoResult = accountClient.addAdminAccount(addVenueAdminForm);
            if (venueAdminDtoResult.getCode() != 200) {
                throw new VenueCertifyException(venueAdminDtoResult.getCode(), venueAdminDtoResult.getMsg());
            }
            //生成认证申请
            VenueCertify venueCertify = BeanUtil.toBean(param, VenueCertify.class);
            venueCertify.setVenueId(venueInfoDto.getVenueId());
            venueCertify.setVenueImgs(JSONUtil.toJsonStr(param.getVenueImgs()));
            venueCertify.setVenueLicense(JSONUtil.toJsonStr(param.getVenueLicense()));
            venueCertifyDao.insert(venueCertify);
        }else {
            List<VenueCertify> collect = venueCertifies.stream().filter(o -> o.getCertifyState() == StateUtil.VENUE_CERTIFY_STATE_NORMAL).collect(Collectors.toList());
            if (collect != null && collect.size() != 0) {
                throw new VenueCertifyException(VenueCertifyExceptionCode.VENUE_CERTIFY_HAD_EXIST);
            }

            //生成认证申请
            VenueCertify venueCertify = BeanUtil.toBean(param, VenueCertify.class);
            venueCertify.setVenueId(venueCertifies.get(0).getVenueId());
            venueCertify.setVenueImgs(JSONUtil.toJsonStr(param.getVenueImgs()));
            venueCertify.setVenueLicense(JSONUtil.toJsonStr(param.getVenueLicense()));
            venueCertifyDao.insert(venueCertify);
        }
        return null;
    }

    @Override
    public void agree(Integer id) {
        VenueCertify venueCertify = venueCertifyDao.selectById(id);
        if (ObjectUtil.isEmpty(venueCertify)) {
            throw new VenueCertifyException(VenueCertifyExceptionCode.VENUE_CERTIFY_NOT_EXIST);
        }
        venueCertify.setCertifyState(StateUtil.VENUE_CERTIFY_STATE_AGREE);
        venueCertifyDao.updateById(venueCertify);
    }

    @Override
    public void refuse(RefuseVenueCertifyParam param) {
        VenueCertify venueCertify = venueCertifyDao.selectById(param.getId());
        if (ObjectUtil.isEmpty(venueCertify)) {
            throw new VenueCertifyException(VenueCertifyExceptionCode.VENUE_CERTIFY_NOT_EXIST);
        }
        venueCertify.setCertifyState(StateUtil.VENUE_CERTIFY_STATE_REFUSE);
        venueCertify.setRefuseCause(param.getReason());
        venueCertifyDao.updateById(venueCertify);
        venueInfoService.removeById(venueCertify.getVenueId());
    }

    @Override
    public PageInfo<VenueCertifyDto> load(LoadVenueCertifyParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        ArrayList<VenueCertifyDto> venueCertifyDtos= venueCertifyDao.load(param);
        PageInfo<VenueCertifyDto> venueCertifyDtoPageInfo = new PageInfo<>(venueCertifyDtos);
        return venueCertifyDtoPageInfo;
    }
}
